/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ShopsDialog.java
 *
 * Created on May 5, 2011, 10:36:34 AM
 */
package homebudget;

import java.util.HashMap;

/**
 *
 * @author KriT
 */
public class ShopsDialog extends javax.swing.JDialog {    

    /** Creates new form ShopsDialog */
    public ShopsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);    
        initComponents();

        DataBaseMySql baza = new DataBaseMySql(HomeBudgetApp.getConnectionString());
        shopsList.setListData(baza.listaSklep().toArray());
        
        deleteShopButton.setVisible(false);
        jButtonEdytuj.setVisible(false);
        jTextFieldEdytuj.setVisible(false);
    }

   

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        shopsList = new javax.swing.JList();
        newShopTextField = new javax.swing.JTextField();
        addShopButton = new javax.swing.JButton();
        deleteShopButton = new javax.swing.JButton();
        jButtonEdytuj = new javax.swing.JButton();
        jTextFieldEdytuj = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        shopsList.setName("shopsList"); // NOI18N
        shopsList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                shopsListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(shopsList);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(homebudget.HomeBudgetApp.class).getContext().getResourceMap(ShopsDialog.class);
        newShopTextField.setText(resourceMap.getString("newShopTextField.text")); // NOI18N
        newShopTextField.setName("newShopTextField"); // NOI18N

        addShopButton.setText(resourceMap.getString("addShopButton.text")); // NOI18N
        addShopButton.setName("addShopButton"); // NOI18N
        addShopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addShopButtonActionPerformed(evt);
            }
        });

        deleteShopButton.setText(resourceMap.getString("deleteShopButton.text")); // NOI18N
        deleteShopButton.setName("deleteShopButton"); // NOI18N
        deleteShopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteShopButtonActionPerformed(evt);
            }
        });

        jButtonEdytuj.setText(resourceMap.getString("jButtonEdytuj.text")); // NOI18N
        jButtonEdytuj.setName("jButtonEdytuj"); // NOI18N
        jButtonEdytuj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEdytujActionPerformed(evt);
            }
        });

        jTextFieldEdytuj.setText(resourceMap.getString("jTextFieldEdytuj.text")); // NOI18N
        jTextFieldEdytuj.setName("jTextFieldEdytuj"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(addShopButton, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                    .addComponent(newShopTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                    .addComponent(deleteShopButton, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                    .addComponent(jButtonEdytuj, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                    .addComponent(jTextFieldEdytuj, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newShopTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(addShopButton)
                        .addGap(39, 39, 39)
                        .addComponent(jTextFieldEdytuj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonEdytuj)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteShopButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addShopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addShopButtonActionPerformed
      //  shopsDB.shops.add(newShopTextField.getText());
       // shopsList.setListData(shopsDB.shops.toArray());
        String nowySklep = newShopTextField.getText();
        DataBaseMySql baza = new DataBaseMySql(HomeBudgetApp.getConnectionString());
        baza.dodajSklep(nowySklep);
       
        
        //aktualizacja danych listy        
        shopsList.setListData(baza.listaSklep().toArray());

        newShopTextField.setText("");
         baza.close();
    }//GEN-LAST:event_addShopButtonActionPerformed

    private void deleteShopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteShopButtonActionPerformed
      //  shopsDB.shops.remove(shopsList.getSelectedIndex());
      //  shopsList.setListData(shopsDB.shops.toArray());

        String deleteSklep = (String)shopsList.getSelectedValue();

        DataBaseMySql baza = new DataBaseMySql(HomeBudgetApp.getConnectionString());
        baza.usunSklep(deleteSklep);
        

        //aktualizacja danych lista
        shopsList.setListData(baza.listaSklep().toArray());

        if (shopsList.getSelectedIndex() == -1) {
            deleteShopButton.setVisible(false);
            jButtonEdytuj.setVisible(false);
        jTextFieldEdytuj.setVisible(false);
        } else {
            deleteShopButton.setVisible(true);
            jButtonEdytuj.setVisible(true);
        jTextFieldEdytuj.setVisible(true);
        }
        baza.close();
    }//GEN-LAST:event_deleteShopButtonActionPerformed

    private void shopsListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_shopsListValueChanged
        if (shopsList.getSelectedIndex() == -1) {
            deleteShopButton.setVisible(false);
            jButtonEdytuj.setVisible(false);
            jTextFieldEdytuj.setVisible(false);
        } else {
            deleteShopButton.setVisible(true);
            jButtonEdytuj.setVisible(true);
        jTextFieldEdytuj.setVisible(true);
        jTextFieldEdytuj.setText((String)shopsList.getSelectedValue());
        }
    }//GEN-LAST:event_shopsListValueChanged

    private void jButtonEdytujActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEdytujActionPerformed
        // TODO add your handling code here:
        String nazwaSklepu="";
        String nowaNazwaSklepu ="";
        DataBaseMySql baza = new DataBaseMySql(HomeBudgetApp.getConnectionString());

        nazwaSklepu = (String)shopsList.getSelectedValue();
        nowaNazwaSklepu = jTextFieldEdytuj.getText();
        baza.edytujSklep(nazwaSklepu, nowaNazwaSklepu);

        //aktualizacja danych listy
        
        shopsList.setListData(baza.listaSklep().toArray());

        if (shopsList.getSelectedIndex() == -1) {
            deleteShopButton.setVisible(false);
            jButtonEdytuj.setVisible(false);
        jTextFieldEdytuj.setVisible(false);
        } else {
            deleteShopButton.setVisible(true);
            jButtonEdytuj.setVisible(true);
        jTextFieldEdytuj.setVisible(true);
        }
        baza.close();
    }//GEN-LAST:event_jButtonEdytujActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ShopsDialog dialog = new ShopsDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addShopButton;
    private javax.swing.JButton deleteShopButton;
    private javax.swing.JButton jButtonEdytuj;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextFieldEdytuj;
    private javax.swing.JTextField newShopTextField;
    private javax.swing.JList shopsList;
    // End of variables declaration//GEN-END:variables

    
}
